package org.ling.xuan.dao;

import cn.hutool.core.util.StrUtil;
import org.ling.xuan.config.DbConfig;
import org.ling.xuan.entity.mo.ProjectSetting;
import org.ling.xuan.entity.row.ProjectSettingRowMapper;
import org.ling.xuan.util.LxStrUtil;

import java.util.Date;
import java.util.List;

public class ProjectDao {

    public static void updateSetting(String basePath, ProjectSetting st) {
        if (added(basePath)) {
            LxStrUtil.unionPath(basePath);
            st.setProjectPath(basePath);
            String sql = "UPDATE `project_setting` " +
                    "SET `local_pre` = ?," +
                    " `svn_pre` = ?, " +
                    "`add_pre` = ?," +
                    " `zip_pre` = ?, " +
                    "`compile_path` = ?," +
                    "`test_compile_path` = ?," +
                    " `build_path` = ?," +
                    " `project_path` = ?," +
                    " `web_path` = ?, " +
                    "`project_name` = ?, " +
                    "`system_name` = ?, " +
                    "`deleted` = 0 WHERE" +
                    " `project_path` = ?";
            DbConfig.getJdbcTemplate().update(sql,
                    LxStrUtil.unionPath(st.getLocalPre()),
                    LxStrUtil.unionPath(st.getSvnPre()),
                    LxStrUtil.unionPath(st.getAddPre()),
                    LxStrUtil.unionPath(st.getZipPre()),
                    LxStrUtil.unionPath(st.getCompilePath()),
                    LxStrUtil.unionPath(st.getTestCompilePath()),
                    LxStrUtil.unionPath(st.getBuildPath()),
                    LxStrUtil.unionPath(st.getProjectPath()),
                    LxStrUtil.unionPath(st.getWebPath()),
                    LxStrUtil.unionPath(st.getProjectName()),
                    LxStrUtil.unionPath(st.getSystemName()),
                    basePath);
        } else {
            String sql = "INSERT INTO `project_setting`" +
                    "(`id`," +
                    " `local_pre`," +
                    " `svn_pre`, " +
                    "`add_pre`," +
                    " `zip_pre`," +
                    " `compile_path`, " +
                    " `test_compile_path`, " +
                    "`build_path`, " +
                    "`deleted`," +
                    " `project_path`," +
                    " `web_path`," +
                    " `project_name`," +
                    " `ctd_time`, " +
                    "`system_name`) VALUES (" +
                    "?, ?, ?, ?, ?, ?, ?,?, ?, ?, ?, ?, ?, ?)";
            DbConfig.getJdbcTemplate().update(sql,
                    StrUtil.uuid(),
                    LxStrUtil.unionPath(st.getLocalPre()),
                    LxStrUtil.unionPath(st.getSvnPre()),
                    LxStrUtil.unionPath(st.getAddPre()),
                    LxStrUtil.unionPath(st.getZipPre()),
                    LxStrUtil.unionPath(st.getCompilePath()),
                    LxStrUtil.unionPath(st.getTestCompilePath()),
                    LxStrUtil.unionPath(st.getBuildPath()),
                    false,
                    LxStrUtil.unionPath(st.getProjectPath()),
                    LxStrUtil.unionPath(st.getWebPath()),
                    LxStrUtil.unionPath(st.getProjectName()),
                    new Date(),
                    LxStrUtil.unionPath(st.getSystemName()));
        }
    }

    public static void delete(String basePath) {
        LxStrUtil.unionPath(basePath);
        DbConfig.getJdbcTemplate().update("delete from project_setting where project_path = ?", basePath);
    }

    public static ProjectSetting getSetting(String basePath) {
        LxStrUtil.unionPath(basePath);
        String sql = "select * from project_setting where project_path = ?";
        List<ProjectSetting> list = DbConfig.getJdbcTemplate().query(sql, ProjectSettingRowMapper.INSTANCE, basePath);
        if (!list.isEmpty()) {
            return list.get(0); // Return the first result if it exists
        } else {
            return null; // Return null if the list is empty
        }
    }

    public static boolean added(String basePath) {
        LxStrUtil.unionPath(basePath);
        String sql = "select count(1) from project_setting where project_path = ? limit 1";
        Integer i = DbConfig.getJdbcTemplate().queryForObject(sql, Integer.class, basePath);
        return i != null && i > 0;

    }

    public static List<ProjectSetting> listAll() {
        String sql = "select * from project_setting";
        return DbConfig.getJdbcTemplate().query(sql, ProjectSettingRowMapper.INSTANCE);
    }

    public static ProjectSetting getByFilePath(String filePath) {
        List<ProjectSetting> projectSettings = listAll();
        for (ProjectSetting ps : projectSettings) {
            if (filePath.startsWith(ps.getProjectPath())) {
                return ps;
            }
        }
        return null;
    }
}
